package com.leonyip.firstdemo.controller;

import com.leonyip.firstdemo.entity.User;
import com.leonyip.firstdemo.service.UserServie;
import com.leonyip.firstdemo.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {
    @Autowired
    private UserServie userServie;

    @GetMapping("/{id}")
    public User getById(@PathVariable Integer id){
        return userServie.getById(id);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Integer id){
        return userServie.delete(id);
    }

    @PostMapping("")
    public int save(@RequestBody User user){
        return userServie.save(user);
    }

    @PutMapping("")
    public int update(@RequestBody User user){
        return userServie.update(user);
    }

    @GetMapping("")
    public PageInfo<User> page(Integer pageNum, Integer pageSize){
        if(pageNum==null){
            List<User> list = userServie.getAll();
            PageInfo<User> pageInfo=new PageInfo<>();
            pageInfo.setPageNum(1);
            pageInfo.setPageSize(10);
            pageInfo.setData(list);
            pageInfo.setTotal((long) list.size());
            return pageInfo;
        }
        return userServie.getByPage(pageNum, pageSize);
    }
}
